package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import control.ControlMain;
import dao.Studentdao;
import entity.Student;

public class UpdFrame extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultTableModel dtm;
	private JTable table;
	private ControlMain cm;
	private JButton jtfadd;
	private JTextField[] stujtf;
	private JTextField seljtf;
	private Studentdao sd;
	private Student s;

	public UpdFrame() {
		setTitle("修改学生信息！");
		setVisible(true);
		setSize(700, 300);
		setResizable(false);
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		dtm=new DefaultTableModel(new Object[][]{}, new String[]{"id","name","sex"});
		table=new JTable(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane jsp = new JScrollPane(table);
		jsp.setSize(228, 199);
		jsp.setLocation(0, 0);
		add(jsp);
		JLabel idjl = new JLabel("输入需要修改学生信息的学生id号：");
		idjl.setSize(220, 30);
		idjl.setLocation(250, 0);
		add(idjl);
		seljtf = new JTextField();
		seljtf.setBounds(455, 0, 50, 30);
		add(seljtf);
		jtfadd = new JButton("Add");
		jtfadd.setSize(60, 25);
		jtfadd.setLocation(550, 2);
		jtfadd.addActionListener(this);

		add(jtfadd);
		JLabel label = new JLabel("请在add之后的文本框内输入修改的信息并且点击保存");
		label.setSize(330, 30);
		label.setLocation(250, 40);
		add(label);
		String[] str = { "id:", "name:", "sex:" };
		JLabel[] stujl = new JLabel[str.length];
		stujtf = new JTextField[str.length];
		for (int i = 0; i < str.length; i++) {
			stujl[i] = new JLabel(str[i]);
			stujl[i].setSize(50, 30);
			stujl[i].setLocation(250, 70 + i * 40);
			stujtf[i] = new JTextField();
			stujtf[i].setSize(80, 30);
			stujtf[i].setLocation(400, 70 + i * 40);
			add(stujl[i]);
			add(stujtf[i]);
		}
		stujtf[0].setEnabled(false);
		String[] jbstr = { "保存", "返回", "关闭" };
		JButton[] updjb = new JButton[jbstr.length];
		for (int i = 0; i < jbstr.length; i++) {
			updjb[i] = new JButton(jbstr[i]);
			updjb[i].setSize(80, 40);
			updjb[i].setLocation(220 + i * 100, 220);
			updjb[i].addActionListener(this);
			add(updjb[i]);
		}
		s=new Student();
		sd=new Studentdao();
		selAllStu();

	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd == "Add") {
			selStu();
		}
		if (cmd == "保存") {
			int f = JOptionPane.showConfirmDialog(this, "确认保存修改信息吗？", "提示信息！", JOptionPane.YES_NO_OPTION);
			if (f == 0) {
				updStu();
				JOptionPane.showMessageDialog(this, "该学生信息已成功修改！");
			} else {
				JOptionPane.showMessageDialog(this, "修改学生信息已取消！");
			}
		}
		if (cmd == "返回") {
			cm = new ControlMain();

			this.setVisible(false);
			cm.setVisible(true);

		}
		if (cmd == "关闭") {
			System.exit(0);
		}

	}

	public void selAllStu() {
		List<Student> stus=sd.selAll();
		for (Student s : stus) {
			dtm.addRow(s.toArray());
		}
	}

	public void selStu() {
		int a=Integer.valueOf(seljtf.getText());
		s=sd.selOne(a);
		System.out.println(seljtf.getText());
		stujtf[0].setText(String.valueOf(s.getId()));
		stujtf[1].setText(s.getName());
		stujtf[2].setText(s.getSex());
	}

	public void updStu() {
		s.setId(Integer.valueOf(stujtf[0].getText()));
		s.setName(stujtf[1].getText());
		s.setSex(stujtf[2].getText());
		
		sd.updStu();
	}

}
